/*******************************************************************
This file is part of iLaunch.

iLaunch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iLaunch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iLaunch.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#include "stdafx.h"
#include <initguid.h>

#include "sMedia.h"
//#include "PhoneUtils.h"
#include "FileUtils.h"
#include "FindFileSorted.h"


// *************************************************
// These functions are for the list of links
// *************************************************

HRESULT sMediaPopulate(DataItem * parent, void (*adder)(DataItem*),
						CSettings * pSettings) {

	HRESULT hr = S_OK;
	//DWORD dwNumberOfBytesWritten = 0;

	TCHAR szNamePath[MAX_PATH] = {0};	//path to be used to get the name without .lnk
	TCHAR szLinkPath[MAX_PATH] = {0};	//path to be used to get the full link
	TCHAR szIconLink[SECONDARY_TEXT_LENGTH] = {0};
	StringCchCopy(szIconLink, SECONDARY_TEXT_LENGTH, TEXT("icon.lnk"));

	// This will hold our "next" list item, which we'll later
	// pass to this->_addListItem
	DataItem data = {0};
	//data.iGroup = 0;
	data.ID = 1;
	data.type = diListItem;

	// localized start menu/programs folder
	// localized folder name (from language file)
	SHGetSpecialFolderPath(NULL, szNamePath, CSIDL_PROGRAMS, false);
	StringCchCat(szNamePath, MAX_PATH, TEXT("\\"));
	StringCchCat(szNamePath, MAX_PATH, pSettings->multimedia_string);
	StringCchCat(szNamePath, MAX_PATH, TEXT("\\"));

   CFindFileSorted *findFileSorted;

   findFileSorted = new CFindFileSorted(szNamePath, _T("*.lnk"));

   for (int i = 0; i < findFileSorted->getCount(); i++)
   {
		StringCchCopy(data.szPrimaryText, PRIMARY_TEXT_LENGTH, findFileSorted->getAt(i));

		// this is the grouping: first char of szprimarytext
		//data.iGroup = (int)_totupper(data.szPrimaryText[0]);

		bool bIsIconLink = false;
		if(CompareString(LOCALE_NEUTRAL, NORM_IGNORECASE, data.szPrimaryText, -1, szIconLink, -1) == CSTR_EQUAL)
			bIsIconLink = true;

		data.szPrimaryText[_tcslen(data.szPrimaryText) - 4] = '\0';

		// see above - bad coding, oh well...
		SHGetSpecialFolderPath(NULL, szLinkPath, CSIDL_PROGRAMS, false);
		StringCchCat(szLinkPath, MAX_PATH, TEXT("\\"));
		StringCchCat(szLinkPath, MAX_PATH, pSettings->multimedia_string);
		StringCchCat(szLinkPath, MAX_PATH, TEXT("\\"));

		StringCchCat(szLinkPath, MAX_PATH, findFileSorted->getAt(i));
		StringCchCopy(data.szLinkPath, MAX_PATH, szLinkPath);
		
		if(!bIsIconLink)
			adder(&data);
	}

	hr = S_OK;

	return hr;
}

HRESULT sMediaClick(DataItem * data, float x,  
					 int * newScreen, CSettings * pSettings) {

	SHELLEXECUTEINFO sei = {0};

	sei.cbSize = sizeof(sei);
	sei.lpVerb = TEXT("Open");
	sei.lpFile = data->szLinkPath;
	sei.lpParameters = TEXT("");
	sei.nShow = SW_SHOW;
	ShellExecuteEx(&sei);

	return S_OK;
}

HRESULT sMediaGetTitle(DataItem * parent, TCHAR * buffer, int cchDest,
						CSettings * pSettings) {

	StringCchCopy(buffer, cchDest, parent->szPrimaryText);
	return S_OK;
}

HRESULT sMediaGetGroup(DataItem * data, TCHAR * buffer, int cchDest,
						CSettings * pSettings) {

	buffer[0] = (TCHAR)data->iGroup;
	buffer[1] = 0;
	return S_OK;
}
